package com.controller;

import com.entity.Blog;
import com.entity.Comment;
import com.entity.User;
import com.service.impl.BlogServiceImpl;
import com.service.impl.CommentServiceImpl;
import com.service.intf.BlogService;
import com.service.intf.CommentService;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class CommentServlet extends BaseServlet {
    private CommentService commentService = new CommentServiceImpl();
    BlogService blogService = new BlogServiceImpl();


    protected void queryCommentByBlogId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);

        List<Comment> comments = commentService.queryCommentByBlogId(blogId);
        req.getSession().setAttribute("comments", comments);

        req.getRequestDispatcher("/pages/blog/blog_detail.jsp").forward(req, resp);
    }

    protected void addComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userName = req.getParameter("userName");
        Integer userId = blogService.queryUserIdByUserName(userName);
        int authorId = WebUtils.parseInt(req.getParameter("authorId"), 0);
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
        String comment = req.getParameter("comment");

        Comment addComment = new Comment(null, userId, authorId, blogId, comment, userName);
        System.out.println(addComment);
        commentService.addComment(addComment);
//        req.getRequestDispatcher("/pages/blog/blog_detail.jsp").forward(req, resp);

        resp.sendRedirect(req.getContextPath() + "/user/commentServlet?action=queryCommentByBlogId&blogId=" + blogId);

    }

    protected void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);

        int commentId = WebUtils.parseInt(req.getParameter("commentId"), 0);
        //博客作者的id
        int authorId = WebUtils.parseInt(req.getParameter("authorId"), 0);
        //评论者的id
        int userId = WebUtils.parseInt(req.getParameter("userId"), 0);
        //当前用户的姓名
        String username = req.getParameter("username");
        //当前用户的id
        Integer currentUserId = blogService.queryUserIdByUserName(username);


        commentService.deleteComment(commentId);

        resp.sendRedirect(req.getContextPath() + "/user/commentServlet?action=queryCommentByBlogId&blogId=" + blogId);
    }



    //判断是否有权限可以删除评论
    protected void isDeleteComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        int commentId = WebUtils.parseInt(req.getParameter("commentId"), 0);

        int blogId = WebUtils.parseInt(req.getParameter("blogId"), 0);
//        int authorId = WebUtils.parseInt(req.getParameter("authorId"), 0);


        Blog blog = blogService.querySingleBlogByBlogId(blogId);
        //博客作者的id
        Integer authorId = blog.getUserId();
        //评论者姓名
        String userName = req.getParameter("userName");

        //评论者的id
        int userId = WebUtils.parseInt(req.getParameter("userId"), 0);
        //当前用户的姓名
        String currentUserName = req.getParameter("currentUserName");
        //当前用户的id
        Integer currentUserId = blogService.queryUserIdByUserName(currentUserName);
        //判断当前用户是不是管理员（0为不是，1为是）
        int flag=0;
//        boolean isDelete =false;
        List<User> adminUsers = (List<User>) req.getSession().getAttribute("adminUsers");

        for (User adminUser : adminUsers) {
            if(currentUserId==adminUser.getId()){
                flag=1;
                break;
            }
        }

        //如果当前用户的id等于博客作者id或等于评论者id，则当前用户可以删除该评论
        if(currentUserId==authorId||currentUserId==userId||flag==1){
//            isDelete=true;
            System.out.println("可以删除");
            commentService.deleteComment(commentId);
        }else {
            System.out.println("不可以删除");
        }

        resp.sendRedirect(req.getContextPath() + "/user/commentServlet?action=queryCommentByBlogId&blogId=" + blogId);

    }


}